home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 10 / FM Towns Free Software Collection 10.iso / ms_dos / tool / fwcp / src / tree.c < prev    next >
Text File  |  1995-03-14  |  3KB  |  181 lines

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #include    <string.h>
  4. #include    <ctype.h>
  5. #include    <jctype.h>
  6. #include    <dos.h>
  7. #include    "dir.h"
  8. #include    "tree.h"
  9.  
  10. #define TRUE        1
  11. #define FALSE        0
  12. #define ERR        (-1)
  13.  
  14. #define    STRLEN        256
  15.  
  16. static    char    tree_buff[STRLEN + 2];
  17.  
  18. /**************
  19. gDIR1
  20. egDIR2...
  21. ebDIR3...
  22. bDIR4
  23. **************/
  24.  
  25. char    *pathtree(DIRTREE *dp)
  26. {
  27.     if ( dp == NULL ) {
  28.     strcpy(tree_buff, "\\");
  29.     return tree_buff;
  30.     }
  31.  
  32.     if ( dp->root == NULL )
  33.     tree_buff[0] = '\0';
  34.     else
  35.     pathtree(dp->root);
  36.  
  37.     strcat(tree_buff, "\\");
  38.     strcat(tree_buff, dp->name);
  39.  
  40.     return tree_buff;
  41. }
  42. static    void    subtree(DIRTREE *dp)
  43. {
  44.     if ( dp == NULL )
  45.     tree_buff[0] = '\0';
  46.     else {
  47.     subtree(dp->root);
  48.     strcat(tree_buff, (dp->next != NULL ? "e" : "  "));
  49.     }
  50. }
  51. char    *strtree(DIRTREE *dp)
  52. {
  53.     subtree(dp->root);
  54.     strcat(tree_buff, (dp->next == NULL ? "b" : "g"));
  55.     strcat(tree_buff, dp->name);
  56.     if ( !dp->flag )
  57.     strcat(tree_buff, "…");
  58.     return tree_buff;
  59. }
  60. void    closetree(DIRTREE *dp)
  61. {
  62.     DIRTREE *tp;
  63.  
  64.     while ( dp != NULL ) {
  65.     closetree(dp->child);
  66.     tp = dp->next;
  67.     free(dp);
  68.     dp = tp;
  69.     }
  70. }
  71. DIRTREE    *opentree(DIRTREE *top)
  72. {
  73.     DIR *dir;
  74.     DIRECT *dp;
  75.     DIRTREE *tp;
  76.     DIRTREE *np;
  77.     DIRTREE *bp = NULL;
  78.  
  79.     if ( (dir = opendir(pathtree(top), 0)) == NULL )
  80.     return top;
  81.  
  82.     while ( (dp = readdir(dir)) != NULL ) {
  83.     if ( root_check(dp->d_name) || !IS_DIR(dp) )
  84.         continue;
  85.     if ( (tp = (DIRTREE *)malloc(sizeof(DIRTREE))) == NULL ) {
  86.         message("opentree malloc error");
  87.         break;
  88.     }
  89.  
  90.     tp->next = tp->child = tp->link = NULL;
  91.     tp->root = top;
  92.     tp->flag = FALSE;
  93.     strcpy(tp->name, dp->d_name);
  94.  
  95.     if ( bp == NULL )
  96.         bp = tp;
  97.     else
  98.         np->next = np->link = tp;
  99.     np = tp;
  100.     }
  101.  
  102.     closedir(dir);
  103.  
  104.     if ( top != NULL ) {
  105.     top->flag = TRUE;
  106.     top->child = bp;
  107.     if ( (tp = bp) != NULL ) {
  108.         while ( tp->link != NULL )
  109.         tp = tp->link;
  110.         tp->link = top->link;
  111.         top->link = bp;
  112.     }
  113.     } else
  114.     top = bp;
  115.  
  116.     return top;
  117. }
  118. int    menutree(int x, int y)
  119. {
  120.     DIRTREE *top = NULL;
  121.     DIRTREE *dp;
  122.     int rc = FALSE;
  123.     int no = 0;
  124.     int n, max;
  125.     char **av;
  126.  
  127.     if ( (top = opentree(top)) == NULL )
  128.     return ERR;
  129.  
  130.     while ( rc == FALSE ) {
  131.     dp = top;
  132.     for ( max = 1 ; dp != NULL ; max++ )
  133.         dp = dp->link;
  134.  
  135.     if ( (av = (char **)malloc(sizeof(char *) * (max + 1))) == NULL )
  136.         return ERR;
  137.  
  138.     sprintf(tree_buff, "%c:\\", _getdrive() + 'A' - 1);
  139.     av[0] = strdup(tree_buff);
  140.  
  141.     dp = top;
  142.     for ( n = 1 ; n < max ; n++ ) {
  143.         av[n] = strdup(strtree(dp));
  144.         dp = dp->link;
  145.     }
  146.     av[n] = NULL;
  147.  
  148.     if ( (no = menu(x, y, no, av)) < 0 )
  149.         rc = ERR;
  150.     else {
  151.         if ( no == 0 ) {
  152.         chcwdir("\\");
  153.         rc = TRUE;
  154.         }
  155.         dp = top;
  156.         for ( n = 1 ; n < no ; n++ )
  157.         dp = dp->link;
  158.         if ( dp->flag ) {
  159.         chcwdir(pathtree(dp));
  160.         rc = TRUE;
  161.         } else {
  162.         opentree(dp);
  163. /***********************************************
  164.         if ( dp->child == NULL ) {
  165.             chcwdir(pathtree(dp));
  166.             rc = TRUE;
  167.         }
  168. ************************************************/
  169.         }
  170.     }
  171.  
  172.     for ( n = 0 ; n < max ; n++ )
  173.         free(av[n]);
  174.     free(av);
  175.     }
  176.  
  177.     closetree(top);
  178.  
  179.     return FALSE;
  180. }
  181.